package com.yss.reportworld.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yss.common.core.domain.TreeNode;
import com.yss.common.core.enums.YssStatusEnum;
import com.yss.common.core.exception.BusinessException;
import com.yss.reportworld.domain.Customer;
import com.yss.reportworld.domain.rule.RCheckType;
import com.yss.reportworld.mapper.rule.RCheckTypeMapper;
import com.yss.reportworld.service.IRCheckTypeService;
import lombok.extern.slf4j.Slf4j;
import com.yss.common.core.utils.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * 规则校验_规则分类Service业务层处理
 *
 * @author yss
 * @date 2023-07-25
 */
@Slf4j
@Service
public class RCheckTypeServiceImpl extends ServiceImpl<RCheckTypeMapper, RCheckType> implements IRCheckTypeService {
    /**
     * 查询规则校验_规则分类列表
     *
     * @param rCheckType 规则校验_规则分类
     * @return 规则校验_规则分类
     */
    @Override
    public List<RCheckType> selectRCheckTypeList(RCheckType rCheckType) {
        QueryWrapper<RCheckType> wrapper = new QueryWrapper();
        wrapper.eq("RW_DB_STATUS", YssStatusEnum.NORMAL.getCode());
        if (StringUtils.isNull(rCheckType)) {
            return this.list(wrapper);
        }
        return this.list(wrapper);
    }

    /**
     * 检验规则的树结构信息
     *
     * @return 检验规则树信息集合
     */
    public List<TreeNode> selectRCheckTypeTreeList() {
        List<TreeNode> list = new ArrayList<>();
        QueryWrapper<RCheckType> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("PARENT_ID,SORTS");
        wrapper.eq("STATUS", YssStatusEnum.NORMAL.getCode());
        wrapper.eq("RW_DB_STATUS", YssStatusEnum.NORMAL.getCode());
        List<RCheckType> listRCheckType = this.list(wrapper);
        if (StringUtils.isEmpty(listRCheckType)) {
            return list;
        }
        for (RCheckType rCheckType : listRCheckType) {
            if (StringUtils.isNull(rCheckType)) {
                continue;
            }
            if (StringUtils.isEmpty(rCheckType.getParentId())) {
                rCheckType.setParentId("0");
            }
        }
        List<String> tempList = listRCheckType.stream().map(RCheckType::getId).collect(Collectors.toList());
        Set<String> set = new HashSet<>();
        for (RCheckType rCheckType : listRCheckType) {
            if (StringUtils.isNull(rCheckType)) {
                continue;
            }
            if (set.contains(rCheckType.getParentId()) || tempList.contains(rCheckType.getParentId())) {
                continue;
            }
            list.addAll(this.recursionTree(rCheckType.getParentId(), listRCheckType));
            set.add(rCheckType.getParentId());
        }
        return list;
    }

    private List<TreeNode> recursionTree(String id, List<RCheckType> listRCheckType) {
        List<TreeNode> list = new ArrayList<>();
        for (RCheckType rCheckType : listRCheckType) {
            if (!id.equals(rCheckType.getParentId())) {
                continue;
            }
            TreeNode treeNode = this.handRCheckTypeToNode(rCheckType);
            List<TreeNode> children = recursionTree(rCheckType.getId(), listRCheckType);
            treeNode.setChildren(children);
            list.add(treeNode);
        }
        return list;
    }

    /**
     * 将业务功能转化成TreeNode
     */
    private TreeNode handRCheckTypeToNode(RCheckType rCheckType) {
        TreeNode node = new TreeNode();
        node.setId(rCheckType.getId());
        node.setParentId(rCheckType.getParentId());
        node.setLabel(rCheckType.getName());
        return node;
    }

    /**
     * 获取一个最明细规则的上级规则类型名称拼接到一起，导出EXECl使用
     *
     * @return
     */
    public static final String TREE_SEPARATOR = "/";

    public Map<String, String> getRuleTypeName() {
        Map<String, String> tileMap = new HashMap<String, String>();
        List<TreeNode> list = this.selectRCheckTypeTreeList();
        if (StringUtils.isEmpty(list)) {
            return tileMap;
        }
        list.forEach(e -> buildPathValue(tileMap, null, e, TREE_SEPARATOR));
        return tileMap;
    }

    private static <T extends TreeNode> void buildPathValue(Map<String, String> tileMap, String parentPath, T treeNode, String separator) {
        String path = null == parentPath ? treeNode.getLabel() : (parentPath + separator + treeNode.getLabel());
        tileMap.put(treeNode.getId(), path);

        List<TreeNode> children = treeNode.getChildren();
        if (StringUtils.isNotEmpty(children)) {
            children.forEach(e -> buildPathValue(tileMap, path, e, separator));
        }
    }


    /**
     * 递归查询所有下级ID集合
     *
     * @param id
     * @return
     * @throws Exception
     */
    public List<String> queryRCheckTypeIds(String id) {
        if (StringUtils.isBlank(id)) {
            throw new BusinessException("获取客户信息失败，请稍后重试！");
        }
        List<String> strList = new ArrayList<>();
        QueryWrapper<RCheckType> wrapper = new QueryWrapper<>();
        wrapper.eq("RW_DB_STATUS", YssStatusEnum.NORMAL.getCode());
        List<RCheckType> list = this.list(wrapper);
        recursionIds(id, strList, list);
        return strList;
    }

    private void recursionIds(String id, List<String> strList, List<RCheckType> list) {
        for (RCheckType rCheckType : list) {
            if (!id.equals(rCheckType.getParentId())) {
                continue;
            }
            strList.add(rCheckType.getId());
            recursionIds(rCheckType.getId(), strList, list);
        }
    }
}
